package com.camps.sdk.decrypt;

import com.camps.sdk.common.ExceptionCode;
import com.camps.sdk.entity.DeviceLinkAddress;
import com.camps.sdk.entity.DeviceUpMessage;
import com.camps.sdk.exception.BaseException;
import com.camps.sdk.utils.EncryptionAndDecryptionUtils;

/**
 * @Author ldaoliang
 * @Date create in 14:53 2021/9/1
 * @Description
 */
public interface AnalyticalData {

    DeviceUpMessage parseData(String originalData);


    /**
     * 解析数据成对象
     * @param data
     * @return
     */
    static DeviceUpMessage parseData2Entity(String data){
        // 1.解密
        String originalData = null;
        try{
            originalData = EncryptionAndDecryptionUtils.decrypt(data);
        }catch (Exception e){
            throw new BaseException(ExceptionCode.DECRYPT_ERROR);
        }
        // 2.转对象
        String[] dataArray = originalData.split(",");

        // 3.构建返回值

        // 4.构建返回对象
        return null;
    }

    /**
     * 将指令解密并且转成数组
     * @param data
     * @return
     */
    default String[] parseCommand(String data){
        // 1.解密
        String originalData = null;
        try{
            originalData = EncryptionAndDecryptionUtils.decrypt(data);
        }catch (Exception e){
            throw new BaseException(ExceptionCode.DECRYPT_ERROR);
        }
        //去除末尾的#
        String command = originalData.substring(0,originalData.length() - 1);
        // 2.转对象
        String[] dataArray = command.split(",");
        return dataArray;
    }


    /**
     * 构建返回的DeviceUpMessage
     * @param data
     * @return
     */
    default DeviceUpMessage structureMessage(Integer  dataType, String messType, boolean needResponse, String resMess, Object data){
       DeviceUpMessage deviceUpMessage=new DeviceUpMessage();
       deviceUpMessage.setData(data);
       deviceUpMessage.setDataType(dataType);
       deviceUpMessage.setMessageType(messType);
       deviceUpMessage.setNeedResponse(needResponse);
       deviceUpMessage.setResponseMessage(resMess);
       return deviceUpMessage;
    }


}
